home *** CD-ROM | disk | FTP | other *** search
/ Alles Voor Internet / Tout Pour Internet / alles voor internet.iso / MacInternet™ / Telnet / NCSA / tn3270 2.4d7 source / NCSA⁄BYU TCP⁄IP / mactools.c < prev    next >
Text File  |  1991-12-18  |  10KB  |  436 lines

  1. /*
  2. *  mactools.c                by Gaige B. Paulsen
  3. *
  4. *  adapted from pctools.c      by Tim Krauskopf
  5. ****************************************************************************
  6. *                                                                          *
  7. *      part of:                                                            *
  8. *      TCP/UDP/ICMP/IP Network kernel for NCSA Telnet                      *
  9. *      by Tim Krauskopf                                                    *
  10. *                                                                          *
  11. *      National Center for Supercomputing Applications                     *
  12. *      152 Computing Applications Building                                 *
  13. *      605 E. Springfield Ave.                                             *
  14. *      Champaign, IL  61820                                                *
  15. *                                                                          *
  16. *                                                                          *
  17. ****************************************************************************
  18. *
  19. *  those generic tool-type things that only work on Macs.
  20. *  includes all hardware-level calls to Ethernet that are unique to the Mac
  21. *
  22. */
  23.  
  24. #include <stdio.h>
  25. #include <String.h>
  26.  
  27. #include <Dialogs.h>
  28. #include <Files.h>
  29. #include <AppleTalk.h>
  30. #include <Events.h>
  31. #include <Memory.h>
  32.  
  33. #include "protocol.h"
  34. #include "data.h"
  35. #include "fastnet.h"
  36. #include "configrec.h"
  37. #include "ethertalk.h"
  38. #include "atalk.h"                /* Dlayer to atalk conversion */
  39. #include "dlayer.h"
  40. #include "ip.h"
  41. #include "ether.h"
  42. #include "elistener.h"
  43. #include "listener.h"
  44. #include "maclook.h"
  45.  
  46. #ifdef MPW
  47. #define    movmem(y,x,z)    memcpy(x,y,z)
  48. #endif
  49.  
  50. /*
  51.  * THE ALL-POWERFUL VARIABLE !!!!!!!!!!
  52.  */
  53.  
  54. int EtherNet
  55.   /*
  56.     specifies hardware interface to use.
  57.     Values are
  58.         -99        -- no hardware, using MacTCP (set up in userd.c)
  59.         -2        -- FastNet
  60.         -1        -- Kinetics Ether SC or Ether SE
  61.         0        -- LocalTalk ("built-in")
  62.         1        -- 3Com EtherLink/NB (or Apple EtherTalk)
  63.         9 .. 14 -- EtherLink/NB in specified NuBus slot
  64.   */
  65.     = 0;        /* default to LocalTalk */
  66.  
  67.  
  68.  
  69. /**********************************************************************/
  70. /*  netarpme
  71. *   send an arp to my address.  arpinterpret will notice any response.
  72. *   Checks for adapters which receive their own broadcast packets.
  73. */
  74. int netarpme
  75.   (
  76.     char *s
  77.   )
  78.   {
  79.     if (EtherNet) {    
  80.         reqarp(s);
  81.     }
  82.     return(0);
  83.   }
  84.  
  85. /*
  86.  * ATdlayersend (ptr, size)
  87.  *  send size from ptr out the AT port
  88.  *    All outgoing conversions done here ! 
  89.  */
  90. ATdlayersend(ptr,size)            /*MAC: replaced */
  91. ARPKT *ptr;
  92. unsigned size;
  93. {
  94.     char *todata;
  95.     AddrBlock *destptr;
  96.     int ptype, tsize;
  97.     char wds[14], header[18];
  98.     MPPParamBlock pb;
  99.     static ATARPKT arpbuffer;
  100.  
  101.     int err;
  102.  
  103.     ptype = AIP;                                    /* Default to sending an IP Packet */
  104.  
  105.     pb.DDPsocket=IPSock;                            /* Sorry.... */
  106.     pb.DDPchecksumFlag=FALSE;                        /* who knows why, but we don't */
  107.     pb.DDPwdsPointer=wds;                            /* Here we go..... */
  108.     pb.MPPcsCode=246;                                /* We want to send a packet */
  109.  
  110.     todata=(char *)ptr + sizeof(DLAYER);            /* Point at the real data... */
  111.     (uint8 *) destptr = &ptr->d.dest[0];            /* Where is the destination ? */
  112.     tsize = size- sizeof(DLAYER);                    /* Get transmission size */
  113.  
  114.     if (!memcmp( destptr, bseed, 6))                    /* Watch for Broadcasts */
  115.         movmem(ATbseed, destptr, 4);
  116.  
  117.     if (ptr->d.type == EARP) {
  118.         ptype=AARP;                                        /* Arp packet */
  119.         arpbuffer.hrd = 3;                                /* ATalk */
  120.         arpbuffer.hln = 4;
  121.         arpbuffer.pln = 4;
  122.         arpbuffer.pro = ptr->pro;
  123.         arpbuffer.op  = ptr->op;
  124.         movmem( &ptr->sha[0], &arpbuffer.sha[0], 4);    /* Move addresses */
  125.         movmem( &ptr->spa[0], &arpbuffer.spa[0], 4);
  126.         movmem( &ptr->tha[0], &arpbuffer.tha[0], 4);
  127.         movmem( &ptr->tpa[0], &arpbuffer.tpa[0], 4);
  128.         todata= (char *) &arpbuffer;
  129.         todata+= sizeof(ATdlayer);                        /* New address to go from */
  130.         tsize = sizeof(ATARPKT)- sizeof(ATdlayer);
  131.         }
  132.         
  133.     BuildDDPwds( wds, header, todata, *destptr, ptype, tsize );
  134.  
  135.     err=PWriteDDP( &pb, FALSE);
  136.     if (err!=0) putln("DDPW Error = %d");
  137. #ifdef SENDNOTICE    
  138.     else putln("send");
  139. #endif
  140.     return(err);
  141.     
  142. }
  143.  
  144. /*****************************************************************
  145.  *     E  T  H  E  R  N  E  T   /   E  T  H  E  R  T  A  L  K    *
  146.  *****************************************************************/
  147.  
  148. struct ETHERWDSstruct {
  149.     short len1;
  150.     DLAYER *ptr1;
  151.     short len2;
  152.     DLAYER *ptr2;
  153.     short term;
  154.     } EWDS;
  155.  
  156. int ETdlayersend            /*MAC: replaced */
  157.   (
  158.     DLAYER *ptr,
  159.     unsigned size
  160.   )
  161. {
  162.     EtParam ET;
  163.     int err;
  164.  
  165.     err = 0;
  166.     EWDS.len1=size;                    /* Could it would it in a WDS? */
  167.     EWDS.ptr1=ptr;
  168.     EWDS.len2=0;
  169.  
  170.     ET.address = &EWDS;
  171.  
  172.     /* err = */ ETcall( &ET, EWrite);
  173.     if (/* err != */ 0)
  174.         putln("ETW Error = %d");
  175. #ifdef SENDNOTICE    
  176.     else putln("send");
  177. #endif
  178.     return(err);
  179.     
  180. }
  181.  
  182. /**********************************************************************/
  183. /*   ETdemux    - Ethernet Version of DEMUX routine
  184. *
  185. *  returns the number of packets demuxed
  186. */
  187.  
  188. int ETdemux
  189.   (
  190.     int all
  191.   )
  192.     {
  193.     unsigned getcode;
  194.     int nmuxed,sizered;
  195.     typedef struct peek {
  196.         unsigned short count;
  197.         unsigned char dest[DADDLEN],
  198.             me[DADDLEN];
  199.         unsigned short type;
  200.     } Dpeek;
  201.  
  202.     Dpeek *firstlook;
  203.  
  204.     nmuxed = 0;
  205.  
  206.     do {                                    /* while all flag is on */
  207.  
  208.         if (bufinfo.bufbig > 0) { /*MAC*/
  209.  
  210.             nmuxed++;
  211.             firstlook = (Dpeek *)(bufinfo.bufread);     /*MAC where packet is */
  212.  
  213.             getcode = firstlook->type;            /* where does it belong? */
  214.  
  215.             switch (getcode) {                    /* what to do with it? */
  216.                 case EARP:
  217.                 case ERARP:
  218.                     arpinterpret((ARPKT *) firstlook->dest); /* handle [R]ARP packet */
  219.                     break;
  220.                 case EIP: 
  221.                     ipinterpret((IPKT *) firstlook->dest);    /* handle IP packet */
  222.                     break;
  223.                 default:
  224.                     break;
  225.             }
  226.  
  227.             sizered=(firstlook->count+sizeof(Dpeek));
  228.             bufinfo.bufbig-=sizered;
  229.             
  230. #ifdef DEBUGONLY
  231.             {
  232.             char s[60];
  233.             sprintf(s,"buf: %d, pkt: %d",bufinfo.bufbig,sizered);
  234.             putln(s);
  235.             }
  236. #endif
  237.             bufinfo.bufread+=(sizered+1) & 0xFFFE;
  238.  
  239.             if (bufinfo.bufbig<0) bufinfo.bufbig=0;
  240.             if (bufinfo.bufread>bufinfo.bufend) bufinfo.bufread=bufinfo.buforg;
  241.             
  242.         }
  243.         else {
  244.  
  245.             if (ETdeafreset()) {
  246.                 /* clear deaf flag, buffer overrun */
  247.                 putln("warning: buffer deaf, restarting");
  248.                 }
  249.  
  250.             all = 0;
  251.             }
  252.  
  253.  
  254.     } while (all);            /* should we look for more to deal with? */
  255.  
  256.  
  257.     return(nmuxed);          /* no packets anymore */
  258.  
  259. }
  260.  
  261. /************************************************************************/
  262. /*  dlayersend
  263. *
  264. *  usage:   err = dlayersend(ptr,size)
  265. *      err = 0 for successful, non-zero error code otherwise
  266. *      ptr is to a dlayer packet header
  267. *      size is the number of bytes total
  268. *
  269. *  This particular dlayer routine is for Ethernet.  It will have to be
  270. *  replaced for any other dlayer.
  271. *
  272. *  Assumes that dlayer-dependent initializations were made in protinit.c
  273. *  to help efficiency.
  274. *
  275. *  Ethernet addresses are resolved at higher levels because they will only
  276. *  need to be resolved once per logical connection, instead of once per
  277. *  packet.  Not too layer-like, but hopefully modular.
  278. *
  279. */
  280.  
  281. int dlayersend
  282.   (
  283.     DLAYER *ptr,
  284.     int size
  285.   )
  286. {
  287.     if (EtherNet== 0)
  288.         return( ATdlayersend(ptr,size));
  289.     if (EtherNet==-2)
  290.         return( FNdlayersend(ptr,size));
  291.     else
  292.         return( ETdlayersend(ptr,size));
  293. }
  294.  
  295. int ATdemux
  296.   (
  297.     int all
  298.   )
  299. {
  300.     unsigned getcode;
  301.     int nmuxed,sizered, count;
  302.     ATARPKT *firstlook;
  303.     static ARPKT arpbuffer;
  304.     nmuxed = 0;
  305.  
  306.     do {                                    /* while all flag is on */
  307.  
  308.         if (bufinfo.bufbig > 0) {
  309.             nmuxed++;
  310.             firstlook = (ATARPKT *)(bufinfo.bufread);     /*MAC where packet is */
  311.  
  312.             getcode = firstlook->d.type;            /* where does it belong? */
  313.             count = firstlook->d.count;
  314.             movmem( firstlook->d.dest, arpbuffer.d.dest,4);
  315.             movmem( firstlook->d.me,   arpbuffer.d.me  ,4);
  316.             
  317.             switch (getcode) {                    /* what to do with it? */
  318.                 case AARP:
  319.                     arpbuffer.d.type=EARP;                            /* Arp packet */
  320.                     arpbuffer.hrd = 1;                                /* Enet */
  321.                     arpbuffer.hln = 6;
  322.                     arpbuffer.pln = 4;
  323.                     arpbuffer.pro = firstlook->pro;
  324.                     arpbuffer.op  = firstlook->op;
  325.                     movmem( &firstlook->sha[0], &arpbuffer.sha[0], 4);        /* Move addresses */
  326.                     movmem( &firstlook->spa[0], &arpbuffer.spa[0], 4);
  327.                     movmem( &firstlook->tha[0], &arpbuffer.tha[0], 4);
  328.                     movmem( &firstlook->tpa[0], &arpbuffer.tpa[0], 4);
  329.                     arpinterpret(&arpbuffer);    /* handle ARP packet */
  330.                     break;
  331.                 case AIP:
  332.                     arpbuffer.d.type=EIP;
  333.                     movmem( &arpbuffer, firstlook, sizeof(DLAYER) );
  334.                     ipinterpret((IPKT *) firstlook);
  335.                     break;
  336.                 default:
  337.                     break;
  338.             }
  339.  
  340.             sizered = count + sizeof(DLAYER);
  341.             bufinfo.bufbig-=sizered;
  342.             bufinfo.bufread+=(sizered+1) & 0xFFFE;
  343.  
  344.             if (bufinfo.bufbig<0) bufinfo.bufbig=0;
  345.             if (bufinfo.bufread>bufinfo.bufend) bufinfo.bufread=bufinfo.buforg;
  346.  
  347.         }
  348.         else 
  349.             all = 0;
  350.  
  351.  
  352.     } while (all);            /* should we look for more to deal with? */
  353.  
  354.  
  355.     return(nmuxed);          /* no packets anymore */
  356.  
  357. }
  358.  
  359. /**********************************************************************/
  360. /*   demux
  361. *      find the packets in the buffer, determine their lowest level
  362. *  packet type and call the correct interpretation routines
  363. *
  364. *  the 'all' parameter tells demux whether it should attempt to empty
  365. *  the input packet buffer or return after the first packet is dealt with.
  366. *
  367. *  returns the number of packets demuxed
  368. */
  369.  
  370. int demux
  371.   (
  372.     int all
  373.   )
  374. {
  375.     if (EtherNet==0)
  376.         return( ATdemux(all));
  377.     if (EtherNet==-2)
  378.         return( FNdemux(all));
  379.     else
  380.         return( ETdemux(all));
  381.  
  382. }
  383.  
  384. initbuffer()
  385.     {
  386.     (Ptr) raw=NewPtr(17000);        /* Added by GBP */
  387.     if (raw==NULL) return(-1);
  388.     bufinfo.bufpt = bufinfo.bufread = bufinfo.buforg
  389.                                          = raw;    /*  start at the beginning */
  390.  
  391.     bufinfo.bufend = raw+14500;            /* leave 2K breathing room, required */
  392.     bufinfo.buflim = 12000;             /* another 2K breathing room */
  393.     bufinfo.bufbig = 0;                    /* Added by GBP */
  394.  
  395.     if (EtherNet==0)
  396.         InitBufPtr(&bufinfo);
  397.     else if (EtherNet != -2) 
  398.         ETInitBufPtr( &bufinfo);
  399.     return(0);
  400. }
  401.  
  402. /***************************************************************************/
  403. /* dlayerinit
  404. *  Do machine dependent initializations of whatever hardware we have
  405. *  (happens to be 3com ethernet board here ) 
  406. */
  407. int dlayerinit
  408.   (
  409.     void
  410.   )
  411. {
  412.  
  413.     if (initbuffer())
  414.         return(-1);
  415.     if (EtherNet==0) 
  416.         return(atopen());
  417.     if (EtherNet==-2)
  418.         return(FNopen());
  419.     else
  420.         return(ETopen());    /* Pass the Type along */
  421. }
  422.  
  423. int dlayershut
  424.   (
  425.     void
  426.   )
  427. {
  428.     if (EtherNet==0)
  429.         return(atclose());
  430.     if (EtherNet==-2)
  431.         return(FNclose());
  432.     else
  433.         return(ETclose());
  434. }
  435.  
  436.